package helloservlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LifeCycleServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1922315362237286875L;

	@Override
	public void init() throws ServletException {
		System.out.println("** 1、Servlet初始化 --> init()");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("** 2、Servlet服务 --> doGet()、doPost()");
		  HttpSession ses = req.getSession();
		  System.out.println("session id ="+ses.getId());
		  ses.setAttribute("key", "值");
		  System.out.println("session id ="+ses.getAttribute("key"));
		  String path = getServletContext().getContextPath();
		  System.out.println("path = "+path);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	public void destroy() {
		System.out.println("** 3、Servlet销毁 --> destory()");
		try {
			Thread.sleep(30000);
		} catch (Exception e) {
		}
	}

}
